<?php

/**
 * alltosun.com lenovo登录接口 index.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址:   http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 张元昊 (zhangyh@alltosun.com) $
 * $Date: 2014-11-5 下午3:26:21 $
 * $Id$
*/
class Action
{
    private $appid;
    private $appkey;
    private $callback;
    private $user_id;
    private $password;
    private $hash;

    public function __construct()
    {
        $openapi = Config::get('openapi');
        $this->appid = $openapi['lenovo'][0];
        $this->appkey = $openapi['lenovo'][1];
        $this->callback = $openapi['lenovo'][2];
        $this->hash = user_helper::random_hash(6);
    }

    public function __call($action = '', $params = array())
    {
        $isBind = Request::Get('bind', 0);

        if(user_helper::get_user_id() && !$isBind) {
            return array('您已经登录过了。', 'error', AnUrl('user/setting'));
        }
        $check_info = 'https://passport.lenovo.com/wauthen2/gateway?lenovoid.action=uilogin&lenovoid.realm='.$this->appid.'&lenovoid.cb='.$this->callback;
        $rfUrl = Request::Get('rfUrl', '');
        if($isBind) {
            $_SESSION['bind'] = $isBind;
            if(!user_helper::get_user_id()) {
                unset($_SESSION['bind']);
                return array('您需要先登录才可以绑定第三方帐号。', 'error', AnUrl('user/login'));
            }
            
        } else {
            $_SESSION['bind'] = 0;
        }

        if(!isset($_SESSION['login_callback'])) {
            $_SESSION['login_callback'] = user_helper::get_jump_url($rfUrl);
        }

        // $check_info = urlencode($check_info);
        Response::redirect($check_info);
    }

    public function callback()
    {
        $code = Request::Get('lenovoid_wust', '');
        if(!$code) {
            return array('对不起，授权信息错误，请重试！', 'error', user_helper::get_jump_url());
        }
        $graph_url = "https://passport.lenovo.com/interserver/authen/1.2/getaccountid?lpsust=".$code."&realm=".$this->appid;
        $data = curl_request($graph_url, 'post');
        $request = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
        if(!$request) {
            return array('对不起，获取用户信息错误，请重试！', 'error', user_helper::get_jump_url());
        }
        $connect_info = (array)$request;
        $connect_info['avatar'] = STATIC_URL.'/images/checkin_default.jpg';
        $connect_info['user_name'] = $connect_info['Username'];
        if(!$connect_info['AccountID']) {
            return array('对不起，授权信息错误，请重试！', 'error', user_helper::get_jump_url());
        } else {
            $connect_user = _uri('connect_lenovo', array('lenovo_id' => $connect_info['AccountID']));
            if(!$connect_user) {
                $info = array(
                    'lenovo_id' => $connect_info['AccountID'],
                    'connect_site_id' => 8,
                    'user_name'       => $connect_info['Username'],
                    'verified'        => $connect_info['verified']
                );
                if($_SESSION['bind']) {
                    if(!user_helper::get_user_id()) {
                        return array('对不起，您还未登录！', 'error', user_helper::get_jump_url());
                    }
                    $info['user_id'] = user_helper::get_user_id();
                    $connect_id = _model('connect_lenovo')->create($info);
                    if($connect_id) {
                        unset($_SESSION['bind']);
                        return array('绑定成功！', 'success', AnUrl('user/setting/account_bind'));
                    }
                }
                $connect_id = _model('connect_lenovo')->create($info);
                $_SESSION['associatedConnectInfo']['id'] = $connect_id;
                $_SESSION['associatedConnectInfo']['type'] = 'lenovo';
                $_SESSION['associatedConnectInfo']['user_info'] = $connect_info;
                Response::redirect(AnUrl('openapi/choose_bind'));
            } else {
                if(!$connect_user['user_id']) {
                    if($_SESSION['bind']) {
                        if(!user_helper::get_user_id()) {
                            return array('对不起，您还未登录！', 'error', user_helper::get_jump_url());
                        }
                        $affected = _model('connect_lenovo')->update($connect_user['id'], array('user_id' => user_helper::get_user_id()));
                        if($affected) {
                            unset($_SESSION['bind']);
                            return array('绑定成功！', 'success', AnUrl('user/setting/account_bind'));
                        }
                    }
                    $_SESSION['associatedConnectInfo']['id'] = $connect_user['id'];
                    $_SESSION['associatedConnectInfo']['type'] = 'lenovo';
                    $_SESSION['associatedConnectInfo']['user_info'] = $connect_info;
                    Response::redirect(AnUrl('openapi/choose_bind'));
                } else {
                    if($_SESSION['bind']) {
                        unset($_SESSION['bind']);
                        return array('此联想帐号已经被其他帐号绑定过了', 'error', AnUrl('user/setting/account_bind'));
                    }
                    $this->user_id = $connect_user['user_id'];
                    $user_info = _uri('user', $this->user_id);
                    $info = array(
                            'last_login_time'  =>    date('Y-m-d H:i:s', time())
                    );
                    _model('user')->update($this->user_id, $info);
                    $return_info = $this->user_login($this->user_id);
                    if($return_info['info'] != 'error') {
                        echo $return_info;
                    } else {
                        return($return_info['msg']);
                    }
                }
            }
        }
    }

    private function user_login($user_id)
    {
        // 本站登录，写入session
        $user_info = _uri('user', $user_id);
        user_helper::session_update($user_info, 'lenovo');

        //同步登录ucenter
        $callback = $_SESSION['login_callback'];
        $return_info = user_helper::uc_login($user_info['uc_id'], '', $callback,0,1);
        return $return_info;
    }
}
?>